package com.myspring.lab.dao;

import com.myspring.lab.entity.User;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.Query;


import javax.annotation.Resource;
import java.util.List;
public class DbUser {
    private SessionFactory sessionFactory;

    public DbUser(){
//        Configuration configuration = new Configuration();
//        configuration.configure("hibernate.cfg.xml");
//        sessionFactory = configuration.buildSessionFactory();
//        DbConnnect dbConnnect = new DbConnnect();
        sessionFactory= DbConnnect.getSessionFactory();
    }
    public boolean verifyUser(User user){
        Session session = sessionFactory.openSession();
        try{
            Query query = session.createQuery("from User where username = ?1");
            query.setParameter(1,user.getUsername());
            List<User> res= query.list();
            User user1 = res.get(0);
            if(user.equals(user1)){
                session.close();
                return true;
            }
        }catch(Exception e){
            e.printStackTrace();
        }
        session.close();
        return false;
    }
    public boolean addUser(User user){
        Session session = sessionFactory.openSession();

        Query query = session.createQuery("from User where username = ?1");
        query.setParameter(1,user.getUsername());
        List<User> userList = query.list();

        if(userList.size()==0){
            session.beginTransaction();
            session.save(user);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    public boolean haveUser(User user){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from User where username = ?1");
        query.setParameter(1,user.getUsername());
        List<User> res= query.list();
        if(res.size()!=0){
            return true;
        }else{
            return false;
        }
    }

    public boolean modifyUser(User user){
        Session session = sessionFactory.openSession();
        //判断有该用户后进行事务修改
        if(this.haveUser(user)){
            session.beginTransaction();
            session.saveOrUpdate(user);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    public boolean deleteUser(User user){
        Session session = sessionFactory.openSession();
        if(this.haveUser(user)){
            session.beginTransaction();
            session.delete(user);
            session.getTransaction().commit();
            session.close();
            return true;
        }
        session.close();
        return false;
    }

    public User[] queryUser(String username){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from User where username like '%"+username+"%'");
        List<User> res = query.list();
        User[] users= new User[res.size()];
        res.toArray(users);
        session.close();
        return users;
    }

    public User[] getAllUser(){
        Session session = sessionFactory.openSession();
        Query query = session.createQuery("from User");
        List<User> res = query.list();
        User[] users = new User[res.size()];
        res.toArray(users);
        session.close();
        return users;
    }



/*    public static void main(String[] args) {
        DbUser dbUser = new DbUser();
        User user = new User();
        user.setUsername("a");
        user.setPassword("a");
        System.out.println(dbUser.verifyUser(user));
        //System.out.println(dbUser.addUser(user));
        User [] users = dbUser.getAllUser();
        for(User i:users){
            System.out.println(i.getUsername()+" "+i.getPassword());
        }
        System.out.println(dbUser.haveUser(user));
        System.out.println(dbUser.modifyUser(user));
//        System.out.println(dbUser.deleteUser(user));
        User [] users1 = dbUser.queryUser("a");
        for(User i:users1){
            System.out.println(i.getUsername()+" "+i.getPassword());
        }
    }*/
}
